package com.nsharmon.l18n;

import java.text.MessageFormat;
import java.util.Locale;
import java.util.ResourceBundle;

public class L18n {
	private static L18n instance = null;
	
	private Locale locale = null;
	private ResourceBundle rb = null;
	
	public L18n() {
		rb = ResourceBundle.getBundle(getPackage() + ".messages");
	}
	
	public static L18n get() {
		if(instance == null) {
			instance = new L18n();
		}
		return instance;
	}
	
	public static String getMessage(String key) {
		return get().rb.getString(key);
	}

	public static String getMessage(String key, Object ... params) {
		return MessageFormat.format(getMessage(key), params);
	}
	
	public static String getMessage(Class<?> clazz, String key) {
		return getMessage(clazz.getName() + "." + key);
	}
	
	public static String getMessage(Class<?> clazz, String key, Object ... params) {
		return MessageFormat.format(getMessage(clazz.getName() + "." + key), params);
	}
	
	public void setLocale(Locale locale) {
		this.locale = locale;
		if(locale != null) {
			rb = ResourceBundle.getBundle(getPackage() + ".messages", locale);
		}
	}
	
	public Locale getLocale() {
		return locale;
	}
	
	
	private String getPackage() {
		return getClass().getPackage().getName();
	}
}
